package com.sxpcwlkj.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sxpcwlkj.common.code.controller.BaseController;
import com.sxpcwlkj.common.utils.AjaxResult;
import com.sxpcwlkj.framework.entity.page.PageQuery;
import com.sxpcwlkj.framework.entity.page.TableDataInfo;
import com.sxpcwlkj.system.entity.bo.SysOssBo;
import com.sxpcwlkj.system.entity.vo.SysOssVo;
import com.sxpcwlkj.system.service.SysOssService;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description TODO
 * @Author sxpcwlkj
 * @Version v1.0.0
 */
@RequestMapping("system/oss")
@RestController
@RequiredArgsConstructor
public class SysOssController extends BaseController {
    private final SysOssService baseService;
    private final FileStorageService fileStorageService;//注入实列

    /**
     * 获取用户列表
     */
    @SaCheckPermission("system:user:list")
    @PostMapping("/list")
    public TableDataInfo<SysOssVo> listPage(SysOssBo bo, PageQuery pageQuery) {
        return baseService.selectPageList(bo, pageQuery);
    }


    /**
     * 获取用户详细信息
     *
     * @return
     */
    @SaCheckPermission("system:user:query")
    @GetMapping("/{id}")
    public AjaxResult<SysOssVo> queryById(@PathVariable Long id) {
        return success(baseService.selectVoById(id));
    }

    /**
     * 获取用户详细信息
     *
     * @return
     */
    @SaCheckPermission("system:user:edit")
    @PutMapping
    public AjaxResult<Boolean> edit(@Validated @RequestBody(required = false) SysOssBo bo) {
        return success(baseService.updateById(bo));
    }

    /**
     * 新增用户
     *
     * @return vo
     */
    @SaCheckPermission("system:user:insert")
    @PostMapping
    public AjaxResult<Boolean> insert(MultipartFile file) {
        FileInfo fileInfo = null;
        fileInfo = fileStorageService.of(file).upload();  //将文件上传到对应地方
        if (fileInfo == null) {
            return AjaxResult.fail("上传失败！");
        }
        SysOssBo sysOssBo = new SysOssBo();
        sysOssBo.setUrl(fileInfo.getUrl());
        sysOssBo.setFileName(fileInfo.getFilename());
        sysOssBo.setOriginalName(fileInfo.getOriginalFilename());
        sysOssBo.setFileSuffix(fileInfo.getExt());
        sysOssBo.setPlatform(fileInfo.getPlatform());
        sysOssBo.setContentType(fileInfo.getContentType());
        sysOssBo.setBasePath(fileInfo.getBasePath());
        sysOssBo.setStatus(0);
        sysOssBo.setSort(0);
        return success(baseService.insert(sysOssBo));
    }

    /**
     * 删除用户
     *
     * @return vo
     */
    @SaCheckPermission("system:user:delete")
    @DeleteMapping("/{id}")
    public AjaxResult<Boolean> delete(@PathVariable Long id) {
        SysOssVo vo = baseService.selectVoById(id);
        if (vo != null) {
            //FileInfo fileInfo = fileStorageService.getByUrl(vo.getUrl());
            boolean delete = fileStorageService.delete(vo.getUrl());
            if (!delete) {
                return AjaxResult.fail("桶删除失败");
            }
        }
        return success(baseService.deleteById(id));
    }

}
